#include "variablestack.h"
#include <Qt>
#include <QtGui>

#include <iostream>
using namespace std;
//
VariableStack::VariableStack(QWidget *parent) 
	: QTableView()
{
	this->setParent(parent);
	
			// --- Options on the display ---
		// -- Vertical Header
		Vertical=new QHeaderView(Qt::Vertical,this);
		Vertical->setHidden(true);
		this->setVerticalHeader(Vertical);
		
		// -- Horizontal Dimmensions
		model = new QStandardItemModel(0,2,this);
		model->setHeaderData(0, Qt::Horizontal, "ID");
		model->setHeaderData(1, Qt::Horizontal,"Value");
		this->setModel(model);
		
		//this->setColumnWidth(1,91);
		
		Horizontal=new QHeaderView(Qt::Horizontal,this);
		Horizontal->setDefaultAlignment(Qt::AlignLeft);
		this->setHorizontalHeader(Horizontal);
		this->setColumnWidth(0,20);
		this->setColumnWidth(1,125);
		Horizontal->setStretchLastSection(true);
		
		this->setMaximumWidth(150);
}

void VariableStack::put(Variable*V){
	Stack.append(*V);
	this->UpdateModel();
}

double VariableStack::pop(){
	Variable V=Stack.takeLast();
	this->UpdateModel();
	return V.Data;
}

void VariableStack::UpdateModel(){
	model = new QStandardItemModel(Stack.size(),2,this);
	
	// -- Horizontal Header
		model->setHeaderData(0, Qt::Horizontal,"ID");
		model->setHeaderData(1, Qt::Horizontal,"Value");
	
	for (int row = 0; row < Stack.size(); ++row){
		QStandardItem *item0=new QStandardItem;
		if(row==Stack.size()-1)item0->setText("X");
		else if(row==Stack.size()-2)item0->setText("Y");
		else if(row==Stack.size()-3)item0->setText("Z");
		else if(row==Stack.size()-4)item0->setText("T");
		item0->setEditable(false);
		model->setItem(row,0, item0);
		
		QStandardItem *item1=new QStandardItem;
		item1->setText(QString::number(Stack.at(row).Data));
		item1->setEditable(false);
		model->setItem(row,1, item1);
	}
	this->setModel(model);
}
//
